使用 Deployment 运行一个无状态应用
创建Deployment
:::tip说明
spec.selector.matchLabels
下的标签必须和 spec.template.metadata.labels
下的标签相同,否则创建的时候会报错 The Deployment "nginx-deployment" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"app":"nginx"}:
selectordoes not match template
labels``
:::
编辑yaml文件
cat > deployment.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx # 必须匹配 .spec.template.metadata.labels
replicas: 2 # 告知 Deployment 运行 2 个与该模板匹配的 Pod
template:
metadata:
labels:
app: nginx # 必须匹配 .spec.selector.matchLabels
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
EOF
创建deployment
$ kubectl apply -f deployment.yaml
deployment.apps/nginx-deployment created
查看deployment
$ kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 2/2 2 2 15s